package com.example.homework8.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.homework8.R;
import com.example.homework8.adapter.RecordAdapter;
import com.example.homework8.adapter.RecordAdapter2;
import com.example.homework8.bean.RecordInfo;
import com.example.homework8.database.PlanDBHelper;
import com.example.homework8.database.RecordDBHelper;
import com.example.homework8.utils.SharedUtil;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MainFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MainFragment extends Fragment implements View.OnClickListener {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    public final  String TAG = "GoodsListActivyt";
    private TextView tv_count;
    private FloatingActionButton addbutton;
    private LinearLayout ll_chananel;
    private  int mCount;
    private RecordDBHelper recordhelper ;
    private PlanDBHelper planhelper;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public MainFragment() {
        // Required empty public constructor
    }


    // TODO: Rename and change types and number of parameters
    public static MainFragment newInstance(String param1, String param2) {
        MainFragment fragment = new MainFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    private String mFirst = "true"; // 是否首次打开
    private ListView listview = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_goods_list, container, false);
        addbutton = view.findViewById(R.id.addbutton);

        listview = view.findViewById(R.id.listview);
        addbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.id_content, new AddRecordFragment(), null)
                        .addToBackStack(null)
                        .commit();
            }
        });
        recordhelper = RecordDBHelper.getInstance(getActivity(), 1);
        recordhelper.openReadLink();
        planhelper = PlanDBHelper.getInstance(getActivity(), 1);
        planhelper.openWriteLink();
        String name = SharedUtil.getIntance(getActivity()).readShared("loginuser","未登录");
        ArrayList<RecordInfo> recordInfoArrayList = recordhelper.query("username =" +name );
        for (RecordInfo recordInfo: recordInfoArrayList) {
            Log.d(TAG, "onCreateView: " + recordInfo.toString());
        }
        RecordAdapter2 recordAdapter2 = new RecordAdapter2(recordInfoArrayList, getActivity());
        Log.d(TAG, "onCreateView: " + recordInfoArrayList.size());
        listview.setAdapter(recordAdapter2);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });


        recordAdapter2.notifyDataSetChanged();
        return  view;
    }



    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.iv_cart){

        }
    }
}